<?php

namespace App\Http\Controllers\Admin;

use App\Http\Repository\ChunkRepository;
use App\Http\Repository\FragmentRepository;
use App\Http\Repository\SortRepository;
use App\Http\Servers\Admin\FragmentServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class FragmentController extends CommonController
{
    //信息碎片列表
    public function fragment(Request $request, FragmentRepository $fragmentRepository, FragmentServer $fragmentServer)
    {

        if ($message = $fragmentServer->changeStatus($request)) {
            return $message;
        }

        if ($message = $fragmentServer->delFragment($request)) {
            return $message;
        }

        $data['fragments'] = $fragmentRepository->getFragmentGroupChunk();

        return view('admin.fragment.fragment', $data);
    }

    //信息碎片添加
    public function fragment_add(Request $request, SortRepository $sortRepository, ChunkRepository $chunkRepository, FragmentServer $fragmentServer)
    {
        if ($request->isMethod('post')) {
            if ($message['message'] = $fragmentServer->noPower()) {
                return back()->with($message);
            }

            $this->validate($request, [
                'chunk' => 'required',
                'name' => 'required',
                'path' => 'required|alpha_dash|unique:fragments',
                'num' => 'required',
                'sort' => 'required',
                'type' => 'required',
            ]);

            $create['chunk'] = $request->chunk;
            $create['name'] = $request->name;
            $create['path'] = $request->path;
            $create['num'] = $request->num;
            $create['type'] = $request->type;
            $create['status'] = $request->has('status') ? 1 : 0;
            $create['is_push'] = $request->has('is_push') ? 1 : 0;
            $create['is_new'] = $request->has('is_new') ? 1 : 0;
            $create['is_hot'] = $request->has('is_hot') ? 1 : 0;

            $result = \App\Fragment::create($create);
            if ($result) {
                foreach (explode(',', rtrim($request->sort, ',')) as $sort) {
                    \App\FragmentSortLines::create(['fragment' => $result->id, 'sort' => $sort, 'chunk' => $request->chunk]);
                }
                $message['message'] = message(true, '添加成功', '信息碎片 ' . $request->name . ' 添加成功!');
            } else {
                $message['message'] = message(false, '添加失败', '信息碎片 ' . $request->name . ' 添加失败!');
            }
            return back()->with($message);
        }

        $data['chunk'] = \App\Chunk::findorfail($request->chunk);

        $data['infoNum'] = $chunkRepository->getInfoNumByChunk($request->chunk);

        $data['sorts'] = $sortRepository->getSortsByX($request->chunk);

        return view('admin.fragment.fragment_add', $data);
    }

    //信息碎片编辑
    public function fragment_edit(Request $request, SortRepository $sortRepository, ChunkRepository $chunkRepository, FragmentServer $fragmentServer)
    {
        if ($request->isMethod('post')) {
            if ($message['message'] = $fragmentServer->noPower()) {
                return back()->with($message);
            }

            $fragment = \App\Fragment::findorfail($request->id);

            $this->validate($request, [
                'id' => 'required',
                'name' => 'required',
                'path' => 'required|alpha_dash|unique:fragments,path,' . $request->id . ',id',
                'num' => 'required',
                'sort' => 'required',
                'type' => 'required',
            ]);

            $update['name'] = $request->name;
            $update['path'] = $request->path;
            $update['num'] = $request->num;
            $update['type'] = $request->type;
            $update['status'] = $request->has('status') ? 1 : 0;
            $update['is_push'] = $request->has('is_push') ? 1 : 0;
            $update['is_new'] = $request->has('is_new') ? 1 : 0;
            $update['is_hot'] = $request->has('is_hot') ? 1 : 0;

            $result = \App\Fragment::where('id', $request->id)->update($update);
            if ($result) {
                \App\FragmentSortLines::where('fragment', $request->id)->delete();
                foreach (explode(',', rtrim($request->sort, ',')) as $sort) {
                    \App\FragmentSortLines::create(['fragment' => $request->id, 'sort' => $sort, 'chunk' => $fragment->chunk]);
                }
                $message['message'] = message(true, '编辑成功', '信息碎片 ' . $request->name . ' 编辑成功!');
            } else {
                $message['message'] = message(false, '编辑失败', '信息碎片 ' . $request->name . ' 编辑失败!');
            }
            return back()->with($message);
        }

        $data['fragment'] = \App\Fragment::findorfail($request->id);
        $data['fragment']->sort = \App\FragmentSortLines::where('fragment', $request->id)
            ->lists('sort')
            ->toArray();

        $data['chunk'] = \App\Chunk::findorfail($data['fragment']->chunk);

        $data['infoNum'] = $chunkRepository->getInfoNumByChunk($data['fragment']->chunk);

        $data['sorts'] = $sortRepository->getSortsByX($data['fragment']->chunk);

        return view('admin.fragment.fragment_edit', $data);
    }
}
